<template>
	<view v-if="fileMans.length > 0">
		<fui-collapse-item @change="change" v-for="(item,index) in fileMans" :key="index">
			<view class="fui-item__box">
				<image src="@/static/user_selct.png" class="fui-logo"></image>
				<text>动火操作员：{{item.userNick}}</text>
			</view>
			<template v-slot:content>
				<view class="fui-descr">账号：{{item.userName}}</view>
				<view class="fui-descr">添加时间：{{item.createTime}}</view>
				<view class="fui-descr">
					<view>
						人脸信息：
					</view>
					<view style="width: 20rpx;" v-if="item.facePath">
						<up-button size="mini" @click="onPreview(item.facePath)" text="查看"></up-button>
					</view>
					<view style="width: 20rpx;" v-if="!item.facePath">
						<up-button @click="noAuth(1)" size="mini" type="error" text="未上传"></up-button>
					</view>
				</view>
				<view class="fui-descr">人脸时间：{{item.faceAuthTime}}</view>
				<view class="fui-descr">
					<view>
						身份信息：
					</view>
					<view style="width: 20rpx;" v-if="item.idInfo">
						<up-button size="mini" @click="onPreview(item.idInfo)" text="查看"></up-button>
					</view>
					<view style="width: 20rpx;" v-if="!item.idInfo">
						<up-button @click="noAuth(1)" size="mini" type="error" text="未上传"></up-button>
					</view>
				</view>
				<view class="fui-descr">
					<view>
						特种作业证：
					</view>
					<view style="width: 20rpx;" v-if="item.specialOperationCertificate">
						<up-button size="mini" @click="onPreview(item.specialOperationCertificate)" text="查看"></up-button>
					</view>
					<!-- 	<view style="width: 20rpx;" v-if="!item.specialOperationCertificate">
						<up-button size="mini" @click="uploadCertificate()" text="上传"></up-button>
					</view> -->
				</view>
				<view class="fui-descr" v-if="item.face == 0">人脸认证：<fui-text color="#FF2B2B" text="未认证" type="danger"></fui-text></view>
				<view class="fui-descr" v-if="item.face == 1">人脸认证：<fui-text color="#465CFF" text="已认证" type="danger"></fui-text></view>
				<view class="fui-descr" v-if="item.face == 0">特种作业证认证：<fui-text color="#FF2B2B" text="未认证" type="danger"></fui-text></view>
				<view class="fui-descr" v-if="item.face == 1">特种作业证认证：<fui-text color="#465CFF" text="已认证" type="danger"></fui-text></view>
				<!-- <view style="width: 90%; margin: 20rpx auto;" v-if="user.power == 1">
					<u-button @click="authCertificate(item)" type="primary">认证此操作员特种作业证书</u-button>
				</view> -->
				<view style="width: 90%; margin: 20rpx auto;">
					<u-button @click="removed(item)" type="error">移除此操作员</u-button>
				</view>
			</template>
		</fui-collapse-item>
	</view>
	<view v-else style="display: flex; align-items: center; justify-content: center; width: 100%;height: 100%;">
		<up-empty mode="data">
		</up-empty>
	</view>
</template>

<script>
	import {
		BASE_IP
	} from '@/common/http/url.js'
	export default {
		data() {
			return {
				fileMans: [],
				deviveId: "",
				user: "",

			}
		},
		methods: {
			uploadCertificate() { //上传动火证书
				let url = BASE_IP + "pc/user/upload/operationcertificate"
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						console.log(res);

						const tempFilePaths = res.tempFilePaths[0];
						// console.log(res)
						// this.upload(url, tempFilePaths)
						let _this = this
						// let img_path = res.tempFilePaths[]
						// res.tempFilePaths.forEach((itme) => {
						// 	console.log(itme.path)
						// 	setTimeout(() => {
						// _this.uploadNew(url, tempFilePaths)
						// 	}, 1500)
						// })
					}
				});
			},
			authCertificate(item) {
				let user = uni.getStorageSync("user")
				console.log(user)
				console.log(item)
			},
			onPreview(url) {
				console.log(url)
				const regex = /\.(pdf)$/i;
				const regexv = /\.(jpg|png|jpeg)$/i;
				const matchPdf = regex.test(url);
				const matchPdfv = regexv.test(url);
				if (matchPdf) {

					let urls = BASE_IP + url
					// console.log(urls)
					let filelist = {
						name: '预览',
						fileSrc: urls,
						isFileDown: true
					}
					uni.navigateTo({
						url: "/pages/common/fileView",
						success: (res) => {
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit("dataFun", {
								data: filelist,
							});
						},
					});
				} else if (matchPdfv) {
					uni.showLoading({
						title: "正在加载"
					})
					let urls = BASE_IP + url
					console.log(urls)
					let arr = []
					arr.push(urls)
					uni.previewImage({
						urls: arr,
						current: arr.length,
						success(r) {
							uni.hideLoading();
							console.log(r)
						},
						fail() {
							uni.hideLoading();
							uni.showToast({
								title: "加载失败",
								icon: "error"
							})
						}
					})
				}
			},
			noAuth(res) {
				if (res == 1) {
					uni.showModal({
						title: "提示",
						content: "请登录此用户账号前往-预收功能-上传人脸信息！",
						showCancel: false
					})
					return
				}
				if (res == 2) {
					uni.showModal({
						title: "提示",
						content: "请登录此用户账号前往-预收功能-上传身份信息！",
						showCancel: false
					})
					return
				}
				if (res == 3) {
					uni.showModal({
						title: "提示",
						content: "请登录此用户账号前往-预收功能-上传特种作业证！",
						showCancel: false
					})
					return
				}
			},
			removed(item) {
				console.log(item)
				console.log(this.deviveId)
				let url = BASE_IP + "pc/device/del/file/" + this.deviveId + "/" + item.uuid
				let user = uni.getStorageSync("user")
				let _this = this
				uni.showModal({
					title: "提示",
					content: "您确定要移除此操作员吗？",
					success: (res) => {
						if (res.confirm) {
							uni.request({
								header: {
									"Authorization": user.token,
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								url: url,
								method: "DELETE",
								success: (r) => {
									if (r.statusCode == 200) {
										console.log(r)
										if (r.data.code == 1) {
											uni.showToast({
												title: "删除成功",
												icon: "success"
											})
											_this.getFileMan(_this.deviveId)
										} else {
											uni.showModal({
												title: "提示",
												content: r.data.msg,
												showCancel: false,
											})
											return
											// uni.showToast({
											// 	title: "删除失败",
											// 	icon: "error"
											// })
										}
									} else {
										uni.showToast({
											title: "操作失败",
											icon: "error"
										})
									}
								},
								fail: () => {
									uni.showToast({
										title: "操作失败",
										icon: "error"
									})
								}
							})
						}
					}
				})
			},
			getFileMan(id) {
				let url = BASE_IP + "pc/device/file/man/" + id
				let user = uni.getStorageSync("user")
				uni.request({
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					success: (res) => {
						// console.log(res)
						if (res.statusCode == 200) {
							if (res.data.code == 1) {
								this.fileMans = []
								let data = JSON.parse(res.data.data)
								for (let index in data) {
									let fileman = JSON.parse(data[index])
									this.fileMans.push(fileman)
								}
								console.log(this.fileMans)
							} else {
								this.fileMans = []
							}
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "获取数据失败",
							icon: "error"
						})
					}
				})
			}
		},
		onLoad(ops) {
			console.log(ops.id)
			uni.setNavigationBarTitle({
				title: "消防操作员"
			})
			this.getFileMan(ops.id)
			this.deviveId = ops.id
			let user = uni.getStorageSync("user")
			this.user = user
		}
	}
</script>

<style scoped>
	.fui-item__box {
		width: 100%;
		padding: 26rpx 32rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.fui-logo {
		width: 48rpx;
		height: 48rpx;
		margin-right: 24rpx;
		display: block;
	}

	.fui-descr {
		width: 100%;
		padding: 32rpx;
		font-size: 28rpx;
		line-height: 52rpx;
		color: #7F7F7F;
		word-break: break-all;
		box-sizing: border-box;
		border-bottom: 1px solid #CDD0D6;
		display: flex;
		justify-content: start;
	}
</style>